package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.StatisticsDao;
import com.example.demo.dao.GridProvinceDao;
import com.example.demo.dao.GridCityDao;
import com.example.demo.pojo.Statistics;
import com.example.demo.service.IStatisticsService;
import com.example.demo.vos.StatisticsPageRequestDto;
import com.example.demo.vos.PageResponseDto;
import com.example.demo.vos.AqiDistributeTotalStatisDto;
import com.example.demo.vos.AqiTrendTotalStatisDto;
import com.example.demo.vos.ProvinceItemTotalStatisDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
@Primary
public class StatisticsServiceImpl implements IStatisticsService {

    @Value("${global-data.province-number}")
    private Integer provinceNumber;

    @Value("${global-data.city-number}")
    private Integer cityNumber;

    @Autowired
    private StatisticsDao statisticsDao;

    @Autowired
    private GridProvinceDao gridProvinceDao;

    @Autowired
    private GridCityDao gridCityDao;


    @Override
    public int saveStatistics(Statistics statistics) {
        return statisticsDao.insert(statistics);
    }

    @Override
    public PageResponseDto<Statistics> listStatisticsPage(StatisticsPageRequestDto request) {
        PageResponseDto<Statistics> response = new PageResponseDto<>();

        // 获取总行数
        int totalRow = statisticsDao.getStatisticsCount(request);
        response.setTotalRow(totalRow);

        // 如果总行数为0，直接返回
        if (totalRow == 0) {
            return response;
        }

        // 计算总页数
        int totalPageNum = (totalRow + request.getMaxPageNum() - 1) / request.getMaxPageNum();
        response.setTotalPageNum(totalPageNum);

        // 计算上一页和下一页
        int pageNum = request.getPageNum();
        response.setPreNum(pageNum > 1 ? pageNum - 1 : pageNum);
        response.setNextNum(pageNum < totalPageNum ? pageNum + 1 : pageNum);

        // 计算查询起始位置
        request.setBeginNum((pageNum - 1) * request.getMaxPageNum());
        List<Statistics> list = statisticsDao.listStatisticsPage(request);

        // 填充返回数据
        response.setPageNum(pageNum);
        response.setMaxPageNum(request.getMaxPageNum());
        response.setList(list);

        return response;
    }

    @Override
    public Statistics getStatisticsById(Integer id) {
        return statisticsDao.getStatisticsById(id);
    }

    @Override
    public List<ProvinceItemTotalStatisDto> listProvinceItemTotalStatis() {
        return statisticsDao.listProvinceItemTotalStatis();
    }

    @Override
    public List<AqiDistributeTotalStatisDto> listAqiDistributeTotalStatis() {
        return statisticsDao.listAqiDistributeTotalStatis();
    }

    @Override
    public List<AqiTrendTotalStatisDto> listAqiTrendTotalStatis() {
        List<AqiTrendTotalStatisDto> paramList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, -1);
            AqiTrendTotalStatisDto dto = new AqiTrendTotalStatisDto();
            dto.setMonth(sdf.format(calendar.getTime()));
            paramList.add(dto);
        }
        return statisticsDao.listAqiTrendTotalStatis(paramList);
    }

    @Override
    public long getAqiCount() {
        return statisticsDao.selectCount(null);
    }

    @Override
    public long getAqiGoodCount() {
        QueryWrapper<Statistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("aqi_id", 2);
        return statisticsDao.selectCount(queryWrapper);
    }

    @Override
    public String getProvinceCoverage() {
        long provinceNum = gridProvinceDao.selectCount(null);
        return String.format("%.2f", (provinceNum / (double) this.provinceNumber) * 100);
    }

    @Override
    public String getCityCoverage() {
        long cityNum = gridCityDao.selectCount(null);
        return String.format("%.2f", (cityNum / (double) this.cityNumber) * 100);
    }

}
